package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsRole;
import com.qf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

@Service
public class RoleServiceimpl implements RoleService {

    @Autowired
    DtsRoleMapper dtsRoleMapper;
    @Autowired
    DtsAdminMapper dtsAdminMapper;

    @Override
    public Set<String> queryByIds(Integer[] roleIds) {
        Set<String> roles = new HashSet<String>();

        if(roleIds==null || roleIds.length==0){
            return roles;
        }

        QueryWrapper<DtsRole> wrapper  = new QueryWrapper<>();
        wrapper.in("id", Arrays.asList(roleIds));
        wrapper.eq("deleted",false);
        wrapper.eq("enabled",true);

        List<DtsRole> list = dtsRoleMapper.selectList(wrapper);
        for(DtsRole role : list){
            roles.add(role.getName());

        }
        return roles;
    }

    @Override
    public List<DtsRole> getAll() {
         List<DtsRole> list = dtsRoleMapper.selectList(null);

         return list;

    }

    @Override
    public Map<String, Object> selectList(int page, int limit, String sort, String order,String roleName) {
        IPage<DtsRole> iPage = new Page<>(page,limit);
        QueryWrapper<DtsRole> wrapper = new QueryWrapper();
        if(roleName!=null){
            wrapper.eq("name",roleName);
        }if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        wrapper.eq("deleted",false);

        dtsRoleMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public void create(DtsRole role) {

        dtsRoleMapper.insert(role);

    }

    @Override
    public void update(DtsRole role) {

        dtsRoleMapper.updateById(role);

    }

    @Override
    public void delete(DtsRole role) {

        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        wrapper.select("role_ids");
        wrapper.eq("deleted",false);
        List<DtsAdmin> list = dtsAdminMapper.selectList(wrapper);
        for (DtsAdmin admin : list) {
            Integer[] roleIds = admin.getRoleIds();
            for (Integer roleId : roleIds) {
                if(roleId.equals(role.getId())) {
                    throw new RuntimeException("该角色被管理员关联，不能删除!!!");
                }
            }
            role.setDeleted(true);
            dtsRoleMapper.updateById(role);

        }

    }

    @Override
    public boolean isSuperAdmin(int roleId) {
         QueryWrapper<DtsRole> wrapper = new QueryWrapper<>();

         wrapper.eq("id",roleId);
        DtsRole role = dtsRoleMapper.selectById(roleId);
        if(role.getName().equalsIgnoreCase("超级管理员")){
            return true;
        }
        return false;
    }


}
